package com.cyan.note.controller.manager;

import com.cyan.note.common.ApiResult;
import com.cyan.note.common.PageResult;
import com.cyan.note.dto.manager.worker.WorkerListDTO;
import com.cyan.note.dto.manager.worker.AvailableWorkerDTO;
import com.cyan.note.dto.manager.worker.WorkerStatsDTO;
import com.cyan.note.entity.Worker;
import com.cyan.note.service.manager.ManagerWorkerService;
import com.cyan.note.util.SecurityUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店长端-打手管理控制器
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/api/manager/workers")
public class ManagerWorkerController {

    @Autowired
    private ManagerWorkerService managerWorkerService;

    /**
     * 分页查询打手列表 (根路径)
     */
    @GetMapping
    public ApiResult<PageResult<WorkerListDTO>> getWorkerListRoot(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String accountStatus,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String skillTags,
            @RequestParam(required = false) String joinDateStart,
            @RequestParam(required = false) String joinDateEnd,
            @RequestParam(required = false) String ratingRange,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "desc") String sortOrder
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerWorkerService.getWorkerList(shopId, page, size, keyword, status, 
                accountStatus, level, skillTags, joinDateStart, joinDateEnd, 
                ratingRange, sortBy, sortOrder);
    }

    /**
     * 分页查询打手列表 (兼容/list路径)
     */
    @GetMapping("/list")
    public ApiResult<PageResult<WorkerListDTO>> getWorkerList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String accountStatus,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String skillTags,
            @RequestParam(required = false) String joinDateStart,
            @RequestParam(required = false) String joinDateEnd,
            @RequestParam(required = false) String ratingRange,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "desc") String sortOrder
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerWorkerService.getWorkerList(shopId, page, size, keyword, status, 
                accountStatus, level, skillTags, joinDateStart, joinDateEnd, 
                ratingRange, sortBy, sortOrder);
    }

    /**
     * 获取打手统计数据
     */
    @GetMapping("/stats")
    public ApiResult<WorkerStatsDTO> getWorkerStats() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerWorkerService.getWorkerStats(shopId);
    }

    /**
     * 查询打手详情
     */
    @GetMapping("/{id}")
    public ApiResult<Worker> getWorkerDetail(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerWorkerService.getWorkerDetail(shopId, id);
    }

    /**
     * 新增打手
     */
    @PostMapping
    public ApiResult<Void> createWorker(@Valid @RequestBody Worker worker) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        worker.setShopId(shopId);
        return managerWorkerService.createWorker(worker, operatorId);
    }

    /**
     * 更新打手信息
     */
    @PutMapping("/{id}")
    public ApiResult<Void> updateWorker(@PathVariable Long id, @Valid @RequestBody Worker worker) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        worker.setId(id);
        worker.setShopId(shopId);
        return managerWorkerService.updateWorker(worker, operatorId);
    }

    /**
     * 删除打手
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteWorker(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerWorkerService.deleteWorker(shopId, id, operatorId);
    }

    /**
     * 查询可用打手列表（用于订单分配）
     */
    @GetMapping("/available")
    public ApiResult<List<AvailableWorkerDTO>> getAvailableWorkers() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerWorkerService.getAvailableWorkers(shopId);
    }

    /**
     * 更新打手状态
     */
    @PutMapping("/{id}/status")
    public ApiResult<Void> updateWorkerStatus(
            @PathVariable Long id,
            @RequestParam String status
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerWorkerService.updateWorkerStatus(shopId, id, status, operatorId);
    }

    /**
     * 更新打手账户状态
     */
    @PutMapping("/{id}/account-status")
    public ApiResult<Void> updateWorkerAccountStatus(
            @PathVariable Long id,
            @RequestParam String accountStatus
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerWorkerService.updateWorkerAccountStatus(shopId, id, accountStatus, operatorId);
    }
}